/********************************************/
  // Creation History
  // --------------------------------------------------------------
  //	Company:			HTSZ
  //	NAME:				RestrictDate.java
  //	BY:					Beyes Xia
  //	DATE:				2004-4-20 12:51
  // --------------------------------------------------------------
/*********************************************/
package tss.weeklytimesheet;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import org.apache.log4j.Logger;

import com.ffpc.sql.ConnectionFactory;
import com.util.JdbcUtil;

public class RestrictDate
{
	private String before_date = "";
	private String after_date = "";
	private Logger logger = Logger.getLogger(RestrictDate.class);
	public RestrictDate()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
		String today=sdf.format(new java.util.Date());

		logger.debug("today:"+today);
			
		String todaySunday = get_sunday(today);	

		logger.debug("todaySunday:"+todaySunday);
			
  		ConnectionFactory connectionFactory = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = " select before_quantity,after_quantity "+
					" from tss.tss_c_restrict";
					
		logger.debug("sql:"+sql);
								
		try
		{
	  		connectionFactory = new ConnectionFactory("fasp_tss");
	  		con = connectionFactory.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				int before=rs.getInt(1);
				int after=rs.getInt(2);
				this.before_date=add_date(todaySunday,before*(-7));
				this.after_date=add_date(todaySunday,after*7);
				
				logger.debug("before_date:"+before_date);
				logger.debug("after_date:"+after_date);
			}
			
		}
		catch(Exception sqle)
		{
			logger.error("RestrictDate: "+sqle.getMessage());
		}
		finally
		{
            try
            {
	  			/*rs.close();
	  			stmt.close();*/  
            	
            	// IISZ added in 2013-07-11 begin
            	JdbcUtil.release(stmt, rs);
            	// IISZ added in 2013-07-11 end
                connectionFactory.finalize();            
            }
            catch (Exception ex) 
            {
                logger.error("Exception:"+ex.getMessage());
            }
        } 
	}

	private String add_date(String date,int n)
	{
		Calendar temp=Calendar.getInstance();
		String sunday="";
		int year=Integer.parseInt(date.substring(0,4));
		int month=Integer.parseInt(date.substring(5,7));
		int day=Integer.parseInt(date.substring(8,10));
		temp.set(year,month-1,day);
		temp.add(Calendar.DATE,n);
		String monthStr="";
		String dayStr="";
		monthStr=(temp.get(Calendar.MONTH)>=9?""+(temp.get(Calendar.MONTH)+1):"0"+(temp.get(Calendar.MONTH)+1));
		dayStr=(temp.get(Calendar.DAY_OF_MONTH)>=10?""+temp.get(Calendar.DAY_OF_MONTH):"0"+temp.get(Calendar.DAY_OF_MONTH));
		sunday=""+temp.get(Calendar.YEAR)+"-"+monthStr+"-"+dayStr;
		return sunday;
	}
	
	private String get_sunday(String date)
	{
		Calendar temp=Calendar.getInstance();
		String sunday="";
		int year=Integer.parseInt(date.substring(0,4));
		int month=Integer.parseInt(date.substring(5,7));
		int day=Integer.parseInt(date.substring(8,10));
		temp.set(year,month-1,day);
		temp.add(Calendar.DATE,1-temp.get(Calendar.DAY_OF_WEEK));
		String monthStr="";
		String dayStr="";
		monthStr=(temp.get(Calendar.MONTH)>=9?""+(temp.get(Calendar.MONTH)+1):"0"+(temp.get(Calendar.MONTH)+1));
		dayStr=(temp.get(Calendar.DAY_OF_MONTH)>=10?""+temp.get(Calendar.DAY_OF_MONTH):"0"+temp.get(Calendar.DAY_OF_MONTH));
		sunday=""+temp.get(Calendar.YEAR)+"-"+monthStr+"-"+dayStr;
		return sunday;
	}
	
	public boolean	isValidDate(String v_date)
	{
  		ConnectionFactory connectionFactory = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		int before_gap = 0;
		int after_gap = 0;
		
		String sql = "Select to_date('"+v_date+"','YYYY-MM-DD')-to_date('"+this.before_date+"','YYYY-MM-DD'),"+
					"to_date('"+this.after_date+"','YYYY-MM-DD')-to_date('"+v_date+"','YYYY-MM-DD') From dual";		
					
		logger.debug("xx Sql:"+sql);							
			
		try
		{
	  		connectionFactory = new ConnectionFactory();
	  		con = connectionFactory.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				before_gap = rs.getInt(1);
				after_gap = rs.getInt(2);
				
				logger.debug("before_gap:"+before_gap);
				logger.debug("after_gap:"+after_gap);
			}
			
		}
		catch(Exception sqle)
		{
			logger.error("RestrictDate isValidDate: "+sqle.getMessage());
		}
		finally
		{
            try
            {
	  			/*rs.close();
	  			stmt.close();*/       
            	// IISZ added in 2013-07-11 begin
            	JdbcUtil.release(stmt, rs);
            	// IISZ added in 2013-07-11 end
                connectionFactory.finalize();            
            }
            catch (Exception ex) 
            {
                logger.error("Exception:"+ex.getMessage());
            }
        } 
        					
		if(before_gap >= 0 && after_gap >=0)
			return true;
		else
			return false;
	}
}
	